package org.example.project

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import kotlinproject.composeapp.generated.resources.Res
import kotlinproject.composeapp.generated.resources._941735022379
import kotlinproject.composeapp.generated.resources._951735022410
import kotlinproject.composeapp.generated.resources._961735022446
import kotlinproject.composeapp.generated.resources._971735022470
import kotlinproject.composeapp.generated.resources._981735022487
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.painterResource

/**
 * 轮播图拆分组件
 *
 * @author Gaoyf
 * @since 2025/1/21
 *
 * @param picList 图片数组
 * @param pageNum 当前页码
 * @param limit 分页限制数量
 * @param maxHeight 最大高度
 * @param minHeight 最小高度
 * @param modifier 修饰符
 */
@Composable
fun Carousel2(
    picList: List<ImageBitmap> = listOf(),
    pageNum: Int = 1,
    limit: Int = 0,
    maxHeight: Int? = null,
    minHeight: Int? = null,
    modifier: Modifier = Modifier
) {

    // 模拟图片数组
    val images = listOf(
        Res.drawable._941735022379,
        Res.drawable._951735022410,
        Res.drawable._961735022446,
        Res.drawable._971735022470,
        Res.drawable._981735022487,
    )
    // 轮播图状态对象
    val pagerState = rememberPagerState(pageCount = { if (limit != 0) limit else images.size })
    //
    val coroutineScope = rememberCoroutineScope()
    // 跳转到指定页码
    coroutineScope.launch { pagerState.scrollToPage(pageNum - 1) }


    // 核心组件 分页器
    HorizontalPager(state = pagerState) { page ->
        Box(
            modifier = Modifier
                .fillMaxWidth(),
            contentAlignment = Alignment.Center
        ) {
            Image(
                painter = painterResource(images[page]),
                contentDescription = null,
                contentScale = ContentScale.Crop,
                modifier = Modifier.fillMaxWidth()
                    .heightIn(min = minHeight?.dp ?: 0.dp, max = maxHeight?.dp ?: Int.MAX_VALUE.dp)
            )

            // 提示语
            if (limit > 0 && page == limit - 1) {
                // 渐变背景色对象
                val gradientBrush = Brush.horizontalGradient(
                    colors = listOf(Color(0xff3578f1), Color(0xff469af6)) // 渐变颜色
                )

                Box(
                    modifier = Modifier
                        .background(gradientBrush, shape = RoundedCornerShape(16.dp))
                        .padding(top = 5.dp, bottom = 5.dp, start = 10.dp, end = 10.dp)
                        .align(Alignment.Center)
                ) {
                    Text(
                        "查看剩余${images.size - limit}张",
                        fontSize = 12.sp,
                        fontWeight = FontWeight.W400,
                        color = Color.White,
                    )
                }
            }
        }
    }

}